package service.impl;

import dbconnection.ConnectionFactory;
import service.UserProperty;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;

/**
 * Created by Administrator on 2016/12/21.
 */
public class UserPropertyImpl implements UserProperty{

    public String userPro(int userId) throws Exception {
        ArrayList<String> arrayList=new ArrayList<String>();
        ConnectionFactory connectionFactory=new ConnectionFactory();
        Connection connection=connectionFactory.getConnection();
        String sql="select *from jc_userproperty where userId=?";
        PreparedStatement preparedStatement;
        preparedStatement=(PreparedStatement)connection.prepareStatement(sql);
        preparedStatement.setInt(1,userId);
        ResultSet resultSet=preparedStatement.executeQuery();
        ResultSetMetaData data = resultSet.getMetaData();
        while(resultSet.next()){
            for(int i=1;i<=data.getColumnCount();i++){
                if(!data.getColumnName(i).equals("id")){
                    if(data.getColumnName(i).equals("houseProperty")){
                        arrayList.add(resultSet.getString(data.getColumnName(i)));
                    }else if(data.getColumnName(i).equals("productPrice")||data.getColumnName(i).equals("housePrice")){
                        continue;
                    }else{
                        arrayList.add(resultSet.getString(data.getColumnName(i)));
                    }
                }
            }
        }
        String property="";
        for(int i=0;i<arrayList.size();i++){
            property+=arrayList.get(i);
            if(i!=arrayList.size()-1)
                property+=",";
        }
        connection.close();
        return property;  //90,100,100,80,2,3,30,30000,'1,2,3','9,8,7',3,'1,2,3,4'
    }

    public String userThings(int userId) throws Exception {
        ArrayList<String> proList=new ArrayList<>();
        ArrayList<String> arrayList=new ArrayList<String>();
        ConnectionFactory connectionFactory=new ConnectionFactory();
        Connection connection=connectionFactory.getConnection();
        String sql="select id,productName from jc_product";
        PreparedStatement preparedStatement;
        preparedStatement=(PreparedStatement)connection.prepareStatement(sql);
        ResultSet resultSet=preparedStatement.executeQuery();
        while (resultSet.next()){
            proList.add(resultSet.getString("id"));
            proList.add(resultSet.getString("productName"));
        }
        sql="select *from jc_userthings where userId=?";
        preparedStatement=(PreparedStatement)connection.prepareStatement(sql);
        preparedStatement.setInt(1,userId);
        resultSet=preparedStatement.executeQuery();
        ResultSetMetaData data = resultSet.getMetaData();
        while(resultSet.next()){
            for(int i=1;i<data.getColumnCount();i++){
                if(!data.getColumnName(i).equals("userId")&&!data.getColumnName(i).equals("id")&&!data.getColumnName(i).equals("goodNum")&&!data.getColumnName(i).equals("badNum")){
                    if(data.getColumnName(i).equals("productId")){
//                         for(int j=0;j<proList.size();j++){
//                             if(j%2==0){
//                                 if(resultSet.getString(data.getColumnName(i)).equals(proList.get(j))){
//                                     arrayList.add(proList.get(j+1));
// //                                    System.out.println(proList.get(j+1));
//                                 }
//                             }
//                         }
                        arrayList.add((Integer.parseInt(resultSet.getString(data.getColumnName(i)))-1)+"");
                    }else{
                        arrayList.add(resultSet.getString(data.getColumnName(i)));
//                        System.out.println("----"+resultSet.getString(data.getColumnName(i)));
                    }
                }
            }
        }
        String things="";
        for(int i=0;i<arrayList.size();i++){
            things+=arrayList.get(i);
            if(i!=arrayList.size()-1)
                things+=",";
        }
        connection.close();
        return things;  //1,100,50,20,0,2,200,20,30,0,3,300,10,40,0
    }
}
